package com.flower.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.flower.pojo.Order;
import com.flower.pojo.OrderItem;

/**
 * 订单模块
 */
public interface IOrderDao {
	/**
	 * 生成订单
	 */
	int insertOrder(Order order);
	
	/**
	 * 展示订单
	 */
	List<Order> selectAllOrders();
	
	/**
	 * 删除订单
	 */
	int deleteOrder(int oid);
	
	/**
	 * 查找某个用户已经付账的订单
	 */
	List<Order> findOrder(@Param("uid") int uid, @Param("ostate") int ostate);
	
	/**
	 * 更新订单状态
	 */
	int updateOstate(@Param("uid") int uid, @Param("ostate") int ostate, @Param("pid") int pid, @Param("onum") String onum);
	/**
	 * 判断数据库中的id是否存在
	 */
	@Select("SELECT" + 
			"	(CASE COUNT(o.oid) WHEN 0 THEN 0 ELSE 1 END)" + 
			" FROM" + 
			"	tb_order o" + 
			" WHERE" + 
			"	o.oid = #{orderId}")
	boolean isexistOrderId(@Param("orderId")String orderId);
	/**
	 * 根据订单id获取订单详情数据
	 */
	@Select("SELECT" + 
			"	o.onum orderNumber," + 
			"	o.tel addresseePhone," + 
			"	o.address addresseeAddress," + 
			"	o.receiver addresseeName," + 
			"	o.time orderTime," + 
			"	(CASE o.ostate " + 
			"		WHEN 0 THEN '未付款' " + 
			"		WHEN 1 THEN '已付款' " + 
			"		WHEN 2 THEN '部分发货' " + 
			"		WHEN 3 THEN '全部发货' " + 
			"		ELSE '未知状态' END) orderStatus," + 
			"	o.ototal orderTotalPrice," + 
			"	oi.pay_count itemPayCount," + 
			"	oi.send_count itemSendCount," + 
			"	p.pname itemName," + 
			"	p.pimage itemImage" + 
			" FROM" + 
			"	tb_order o" + 
			"	LEFT JOIN tb_product p ON o.pid = p.pid" + 
			"	LEFT JOIN tb_order_item oi ON oi.order_id = o.oid" + 
			" WHERE o.oid = #{orderId}")
	Map<String, Object> findOrderById(@Param("orderId")String orderId);
	/**
	 * 查询所有订单的详情（用于我的订单）
	 */
	@Select("SELECT" + 
			"	o.onum orderNumber," + 
			"	o.tel addresseePhone," + 
			"	o.address addresseeAddress," + 
			"	o.receiver addresseeName," + 
			"	(CASE o.ostate " + 
			"		WHEN 0 THEN '未付款' " + 
			"		WHEN 1 THEN '已付款' " + 
			"		WHEN 2 THEN '部分发货' " + 
			"		WHEN 3 THEN '全部发货' " + 
			"		ELSE '未知状态' END) orderStatus," + 
			"	p.pname itemName," + 
			"	p.pid itemPid," + 
			"	p.pimage itemImage" + 
			" FROM" + 
			"	tb_order o" + 
			"	LEFT JOIN tb_product p ON o.pid = p.pid" + 
			" WHERE o.uid = #{userId}")
	List<Map<String, Object>> findOrderByUId(@Param("userId")int uid);
	/**
	 * 校验商品发货数量
	 */
	@Select("SELECT" + 
			"	(CASE WHEN p.num < #{itemSendCount} THEN 0 WHEN oi.pay_count < #{itemSendCount} THEN 0 ELSE 1 END) " + 
			" FROM" + 
			"	tb_order o" + 
			" LEFT JOIN tb_product p ON p.pid = o.pid" + 
			" LEFT JOIN tb_order_item oi ON oi.order_id = o.oid" + 
			" WHERE o.oid = #{orderId}")
	boolean checkItemSendCount(
			@Param("orderId")String orderId, 
			@Param("itemSendCount")Integer itemSendCount);
	/**
	 * 修改订单状态,商品库存,订单发货数量
	 * @param orderId
	 * @param itemSendCount
	 * @param integer 
	 */
	@Update("UPDATE " + 
			"	tb_order o" + 
			" LEFT JOIN tb_product p ON p.pid = o.oid " + 
			" LEFT JOIN tb_order_item oi ON oi.order_id = o.oid" + 
			" SET "+ 
			"	o.ostate = #{orderStatus},"+ 
			"	p.num = p.num - #{itemSendCount},"+ 
			"   oi.send_count = #{itemSendCount}" + 
			" WHERE" + 
			"	o.oid = #{orderId}")
	void updateOrderAndProductAndOrderItemByOrderIdAndItemSendCount(
			@Param("orderId")String orderId, 
			@Param("orderStatus")Integer orderStatus,
			@Param("itemSendCount")Integer itemSendCount);
	/**
	 * 根据id修改订单状态
	 * @param orderId
	 * @param orderStatus
	 */
	@Update("UPDATE " + 
			"	tb_order o" + 
			" SET " + 
			"	o.ostate = #{orderStatus}" + 
			" WHERE" + 
			"	o.oid = #{orderId}")
	void updateOrderStatusByOrderId(
			@Param("orderId")String orderId, 
			@Param("orderStatus")Integer orderStatus);
	/**
	 * 根据订单id获取订单购买量
	 * @param orderId
	 * @return
	 */
	@Select("SELECT" + 
			"	IFNULL(oi.pay_count,0)" + 
			" FROM" + 
			"	tb_order o" + 
			" LEFT JOIN tb_order_item oi ON oi.order_id = o.oid" + 
			" WHERE o.oid = #{orderId}")
	int findItemPayCountByOrderId(@Param("orderId")String orderId);
	
	/**
	 * 根据订单id获取发货数量
	 * @param orderId
	 * @return
	 */
	@Select("SELECT" + 
			"	IFNULL(oi.send_count,0)" + 
			" FROM" + 
			"	tb_order o" + 
			" LEFT JOIN tb_order_item oi ON oi.order_id = o.oid" + 
			" WHERE o.oid = #{orderId}")
	int findItemSendCount(@Param("orderId")String orderId);
	/**
	 * 根据订单id获取订单状态
	 * @param orderId
	 * @return
	 */
	@Select("SELECT" + 
			"	o.ostate " + 
			" FROM" + 
			"	tb_order o" + 
			" WHERE" + 
			"	o.oid = #{orderId}")
	int findOrderStatus(@Param("orderId")String orderId);
	@Insert("INSERT INTO tb_order_item" + 
			" VALUE" + 
			" (" + 
			"	#{orderItem.id}," + 
			"	#{orderItem.createTime}," + 
			"	#{orderItem.updateTime}," + 
			"	#{orderItem.payCount}," + 
			"	#{orderItem.sendCount}," + 
			"	#{orderItem.orderId}" + 
			" )")
	int saveOrderItem(@Param("orderItem")OrderItem orderItem);

}
